package com.bai.localshop.controller;


import com.bai.localshop.pojo.dto.UserDTO;
import com.bai.localshop.pojo.dto.UserLoginDTO;
import com.bai.localshop.pojo.dto.UserRegisterDTO;
import com.bai.localshop.pojo.dto.UserUpdateDTO;
import com.bai.localshop.pojo.vo.UserLoginVO;
import com.bai.localshop.result.Result;
import com.bai.localshop.service.AuthService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * 认证控制器，处理登录、注册等认证相关功能
 */
@RestController
@RequestMapping("/api/auth")
@Slf4j
@RequiredArgsConstructor
public class AuthController {

    private final AuthService authService;

    /**
     * 根据id获取当前用户信息
     */
    @GetMapping("/{id}")
    public Result<UserDTO> getCurrentUser(@PathVariable("id") Long id) {
        UserDTO userDTO = authService.getUserById(id);
        return Result.success(userDTO);
    }

    /**
     * 判断用户名是否存在
     */
    @GetMapping("has-username")
    public Result<Boolean> hasUsername(@RequestParam("username") String username) {
        return Result.success(authService.hasUsername(username));
    }

    /**
     * 用户注册
     *
     * @param requestParam 注册参数
     * @return 注册结果
     */
    @PostMapping("/register")
    public Result<String> register(@RequestBody UserRegisterDTO requestParam) {
        authService.register(requestParam);
        return Result.success("注册成功");
    }
    /**
     * 修改用户
     */
    @PutMapping("update")
    public Result<String> update(@RequestBody UserUpdateDTO requestParam){
        authService.update(requestParam);
        return Result.success("修改成功");
    }

    /**
     * 用户登录
     *
     * @param requestParam 登录参数
     * @return 登录结果
     */
    @PostMapping("/login")
    public Result<UserLoginVO> login(@RequestBody UserLoginDTO requestParam) {
        UserLoginVO userLoginVO = authService.login(requestParam);
        return Result.success(userLoginVO);
    }
    /**
     * 检查用户是否登录
     */
    @GetMapping("check-login")
    public Result<Boolean> checkLogin() {
        return Result.success(authService.checkLogin());
    }

    /**
     * 用户登出
     *
     * @return 登出结果
     */
    @DeleteMapping("/logout")
    public Result<String> logout() {
        authService.logout();
        return Result.success("退出成功");
    }
}